//
//  ATKit+UICollectionViewCell.swift
//  ATKit_Swift
//
//  Created by wangws1990 on 2020/12/3.
//  Copyright © 2020 wangws1990. All rights reserved.
//

import UIKit
public extension UICollectionViewCell{
    static var confiTag : Int = 32241981
    class func cellForCollectionView(collectionView:UICollectionView,indexPath : IndexPath) -> Self{
        return self.cellForCollectionView(collectionView: collectionView, indexPath: indexPath, identifier:nil, config: nil)
    }
    class func cellForCollectionView(collectionView:UICollectionView,indexPath: IndexPath,identifier:String? = nil,config:((_ cell :UICollectionViewCell) ->())? = nil) ->Self{
        let identy   = cellRegister(collectionView: collectionView, collectionCell: self.self)
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:identy, for: indexPath)
        if cell.tag != confiTag {
            cell.tag = confiTag
            if let config = config {
                config(cell)
            }
        }
        return cell as! Self
    }
    fileprivate class func cellRegister<T :UICollectionViewCell>(collectionView:UICollectionView,collectionCell :T.Type,identifier :String? = nil) ->String{
        let identy  = identifier ?? NSStringFromClass(collectionCell.classForCoder())
        var res1 = false,res2 = false
        if let dic1 = collectionView.value(forKeyPath: "cellNibDict") as? NSDictionary{
            res1 = (dic1.value(forKeyPath:identy) != nil) ? true : false
        }
        if let dic2 = collectionView.value(forKeyPath: "cellClassDict") as? NSDictionary{
            res2 = (dic2.value(forKeyPath: identy) != nil) ? true : false
        }
        let hasRegister : Bool = res1 || res2
        if hasRegister  == false {
            if let _ = Bundle.main.url(forResource:collectionCell.xibName(), withExtension:"nib"){
                collectionView.register(UINib(nibName: collectionCell.xibName(), bundle: nil), forCellWithReuseIdentifier: identy)
            }else{
                collectionView.register(collectionCell.classForCoder(), forCellWithReuseIdentifier: identy)
            }
        }
        return identy
    }
}
public extension UICollectionReusableView{
    static var configTag : Int = 32241982;
    class func viewForCollectionView(collectionView:UICollectionView,elementKind:String,indexPath:IndexPath) ->Self{
        return viewForCollectionView(collectionView: collectionView, elementKind: elementKind, indexPath: indexPath, identifier: nil, config: nil)
    }
    class func viewForCollectionView(collectionView:UICollectionView,elementKind:String,indexPath:IndexPath,identifier:String? = nil,config:((_ cell :UICollectionReusableView) ->())? = nil) ->Self {
        assert(elementKind.count > 0)
        let identy = viewRegister(collectionView: collectionView, elementKind: elementKind, indexPath: indexPath, identifier: identifier)
        let cell : UICollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: elementKind, withReuseIdentifier: identy, for: indexPath)
        if cell.tag != configTag{
            cell.tag = configTag
            if let config = config {
                config(cell)
            }
        }
        return cell as! Self
    }
    private class func viewRegister(collectionView:UICollectionView,elementKind:String,indexPath:IndexPath,identifier:String? = nil) ->String{
        let identy  = identifier ?? NSStringFromClass(self.classForCoder())
        let keyPath = elementKind.appendingFormat("/" + identy)
        var res1 = false,res2 = false
        if let dic1 = collectionView.value(forKeyPath:"supplementaryViewNibDict") as? NSDictionary{
            res1 = dic1.value(forKeyPath:keyPath) != nil ? true : false
        }
        if let dic2 = collectionView.value(forKeyPath:"supplementaryViewClassDict") as? NSDictionary{
            res2 = dic2.value(forKeyPath:keyPath) != nil ? true : false
        }
        let hasRegister = res1 || res2
        if hasRegister == false {
            if let _ = Bundle.main.url(forResource:self.xibName(), withExtension:"nib"){
                collectionView.register(UINib(nibName: self.xibName(), bundle:nil), forSupplementaryViewOfKind: elementKind, withReuseIdentifier:identy)
            }else{
                collectionView.register(self.classForCoder(), forSupplementaryViewOfKind: elementKind, withReuseIdentifier:identy)
            }
        }
        return identy
    }
}

public extension UICollectionView{
    func cellForCollectionView<T :UICollectionViewCell>(collectionCell :T.Type,indexPath: IndexPath) ->T{
        let identy = UICollectionViewCell.cellRegister(collectionView: self, collectionCell: collectionCell)
        let cell = self.dequeueReusableCell(withReuseIdentifier:identy, for: indexPath)
        return cell as! T
    }
}
